package com.uep.controller;

import com.alibaba.fastjson.JSON;
import com.uep.entity.UserInformation;
import com.uep.service.UserInformationService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by XQ on 2017/2/9.
 */
@RestController
public class UserInformationController {
    private static final Logger LOGGER = Logger.getLogger(UserInformationController.class);
    @Autowired
    UserInformationService userInformationService;

    @ResponseBody
    @RequestMapping(value = "users/allUserInfo/{userAccount}",method = RequestMethod.GET)
    public UserInformation findAll(@PathVariable String userAccount){
        return userInformationService.findUserInformation(userAccount);
    }

    /**
     * 根据用户账户获取账户信息(姓名,性别,年龄,地址,电话)
     * @param userAccount
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "users/userAccount/{userAccount}",method = RequestMethod.GET)
    public UserInformation getPersonInformation(@PathVariable String userAccount){
        return userInformationService.findPersonInformation(userAccount);
    }

    /**
     * 更新用户的个人信息
     * @param userInformation
     */
    @RequestMapping(value = "/userAccount",method = RequestMethod.POST)
    public void updateUser(@RequestBody UserInformation userInformation){
        LOGGER.info("-------->>>>>>>>>>>"+ JSON.toJSON(userInformation));
        UserInformation userInfo = userInformationService.findUserInformation(userInformation.getUserAccount());
        userInfo.setUserName(userInformation.getUserName());
        userInfo.setUserSex(userInformation.getUserSex());
        userInfo.setUserAge(userInformation.getUserAge());
        userInfo.setUserContract(userInformation.getUserContract());
        userInfo.setUserEmail(userInformation.getUserEmail());
        userInformationService.update(userInfo);
    }

    /**
     * 用户注册
     * @param userInformation
     */
    @RequestMapping(value = "/logIn",method = RequestMethod.POST)
    public void logIn(@RequestBody UserInformation userInformation){
        LOGGER.info("-------->>>>>>>>>>>" + JSON.toJSON(userInformation));
        userInformationService.logIn(userInformation);
    }
}
